﻿using System.Collections.Generic;
using Newtonsoft.Json;

namespace LinqToOwin.Web.Labs.Data
{
  public sealed class Tweet
  {
    public string UserName
    {
      get;
      private set;
    }

    public string Location
    {
      get;
      private set;
    }

    public string Text
    {
      get;
      private set;
    }

    private Tweet(string userName, string location, string text)
    {
      UserName = userName;
      Location = location;
      Text = text;
    }

    internal static IEnumerable<Tweet> Parse(string json)
    {
      var tweets = JsonConvert.DeserializeAnonymousType(
        json,
        new
        {
          statuses = new[]
          {
            new
            {
              text = string.Empty,
              user = new
              {
                name = string.Empty,
                location = string.Empty
              }
            }
          }
        });

      foreach (var tweet in tweets.statuses)
      {
        yield return new Tweet(tweet.user.name, tweet.user.location, tweet.text);
      }
    }
  }
}